import requests
import time
import logging
from django.shortcuts import render
from django.views.decorators.http import require_http_methods
from django.http import JsonResponse
from rest_framework import viewsets
from rest_framework.decorators import api_view
from rest_framework.response import Response
from rest_framework.reverse import reverse
from inner.serializers import InnerSerializer
from inner.models import Inner
from playlist.models import Playlist
from playlist.serializers import PlaylistSerializer
# from .permissions import IsAdminOrReadOnly

logging.basicConfig(filename='mple.log',level=logging.DEBUG)

class InnerViewSet(viewsets.ModelViewSet):
    queryset = Inner.objects.all()
    serializer_class = InnerSerializer


class PlaylistViewSet(viewsets.ModelViewSet):
    queryset = Playlist.objects.all()
    serializer_class = PlaylistSerializer


@api_view(['GET'])
def api_root(request, format=None):
    return Response({
        'inner': reverse('inner_list', request=request, format=format),
        'playlist': reverse('playlist_list', request=request, format=format)
    })

def done(request):
    return render(request, "success.html")

def common(request):
    return render(request, "base.html")

def details(request):
    return render(request, "details.html")

def unlock(request):
    return render(request, "unlock.html")

def open_lock(userid, accesstoken):
    headers = {
        'accesstoken': accesstoken,
        'platform': '99'
    }
    url = (
        'http://api.test1.mobike.io/mobike-api/' + 'rentmgr/' + 'unlockBike.do?bikecode=' +
        '7550101183' + '&latitude=' + '22.4674679823975' +
        '&longitude=' + '113.897565120291' + '&timestamp=' +
        str(int(time.time())) + '&citycode=' + '010' +
        '&userid=' + userid + '&channel=0')
    r = requests.post(url=url, headers=headers)
    logging.debug(r.text)
    return r.json()

def open(request):
    url = (
        'http://api.test1.mobike.io/mobike-api/' + 'usermgr/' +
        'partnerlogin.do?mobileNo=' + '15521308687' +
        '&access_token=' + 'eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJzY29wZSI6WyJyZWFkIiwid3JpdGUiXSwiZXhwIjoxNDk3NzEyNTM2LCJhdXRob3JpdGllcyI6WyJST0xFX1RSVVNURURfQ0xJRU5UIl0sImp0aSI6ImQyNzU2NmE4LTJmMGYtNGI3Yy05ZTc0LTk1MzBjZGQ2MmVlOCIsImNsaWVudF9pZCI6InRlYW05In0.cmImWDyfFoYfPqwMGKBykmp2umuHPwq19AhQY-JOp1YPHgn4y_4dMgnWbOjJUn-UH0YNuIpFG_R38wcQs5QzhD-q2WLPdqItfeoy81sWUXQpcPlA0fCFs-S1A5tWTjQ5UM6LCcjDZL_O6fD1WKuwm_wHaTl9Hdc2v1jK8peGI0mHPDOQwBU0lf0TJWU9xUHbwuOzpKgAm_aUIYXildyt6TYtyopKK6Czrz5eA-b6tDNqkrC3spqF_wbcxVtgxmc_oUq7VBGS3JDdMHyuiei7AA-_CPmE0akLYuHunmate7Y6TbsHfuNtkdYnvcvqUmWSjrYYRtdY2lDCeTQjF6pvEQ')
    r = requests.post(url=url)
    userid = r.json()['object']['userid']
    accesstoken = r.json()['object']['authtoken']
    open_lock(userid, accesstoken)
    return 



def fake(request):
    json = {"results": [
        {
        "challengeId": 1,
        "name": "Starbucks Bike Master",
        "sponsorName": "Starbucks",
        "sponsorImageUrl": "https://git.oschina.net/kekeblom/bikechain/raw/master/static/imgs/starbucks.png",
        "description": "Who will make the most trips in one week? Ask your friend to join!",
        "imageUrl": "https://git.oschina.net/kekeblom/bikechain/raw/master/public/images/starbucks.png",
        "startDate": 1497692703371,
        "endDate": 1497699703371,
        "scoreToWin": 10,
        "createdAt": 1497692703371,
        "updatedAt": 1497692703371
        }, 
        {
        "challengeId": 2,
        "name": "Airbnb Mobike Home",
        "sponsorName": "Starbucks",
        "sponsorImageUrl": "",
        "description": "Let's find out how many airbnb house you can go by mobike?",
        "imageUrl": "https://git.oschina.net/kekeblom/bikechain/raw/master/public/images/airbnb.jpg",
        "startDate": 1497692703371,
        "endDate": 1497699703371,
        "scoreToWin": 10,
        "createdAt": 1497692703371,
        "updatedAt": 1497692703371
        }, 
        {
        "challengeId": 3,
        "name": "BurgerKing save your health",
        "sponsorName": "Starbucks",
        "sponsorImageUrl": "\\/resources\\/images\\/starbucks.png",
        "description": "How to save your health with mobike?",
        "imageUrl": "https://git.oschina.net/kekeblom/bikechain/raw/master/public/images/burgerking.png",
        "startDate": 1497692703371,
        "endDate": 61458883200000,
        "scoreToWin": -1,
        "createdAt": 1497692703371,
        "updatedAt": 1497692703371
        },
        {
        "challengeId": 4,
        "name": "Github mobike for programmer",
        "sponsorName": "Github",
        "sponsorImageUrl": "\\/resources\\/images\\/starbucks.png",
        "description": "You need to do some exercise!",
        "imageUrl": "https://git.oschina.net/kekeblom/bikechain/raw/master/public/images/github.png",
        "startDate": 1497692703371,
        "endDate": 61458883200000,
        "scoreToWin": -1,
        "createdAt": 1497692703371,
        "updatedAt": 1497692703371
        }
    ]}
    return JsonResponse(json)
